package dicky.fortunetelling;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	public String tag=MainActivity.class.getSimpleName();
	private long exitTime = 0; 
	private ViewAdapter dataAdapter;
	private String titleary[]=new String[]{
				"数字测算",
				"起名测算",
				"姓名测算",
				"人体生物钟",
				"八字查询",
				"称骨查询",
				"男女五行婚配",
				"关于"
			};
	private String textary[]=new String[]{
				"通过最后4位数字测算吉凶，如电话号码、门牌号、车牌号，不足4位的直接输入整个数字",
				"通过输入姓名的康熙笔画数测算名字的五格及三才吉凶",
				"通过输入姓名测算名字的笔画数及其五格和三才吉凶",
				"通过输入出生日期、测算日期去计算该日期一段范围内的智力、情绪、体力周期值高低",
				"通过输入出生日期、时间计算八字、生肖、农历等",
				"通过输入出生日期、时间计算骨格重量及称骨论命等",
				"通过输入男女双方的出生日期、时间计算五行婚配好坏",
				""
			};
	private Class classary[]=new Class[]{
				numberfortune.class,
				namefortuneTab.class,
				nametofortune.class,
				bodyclock.class,
				bazi.class,
				Saidbone.class,
				fivemarry.class,
				about.class
			};
	private int listcount=titleary.length;
	private TextView title,text;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /*
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent nexpActivityIntent = new Intent();
				nexpActivityIntent.setClass(MainActivity.this,numberfortune.class);
				startActivity(nexpActivityIntent);
			}
		});
        
        btn2=(Button)findViewById(R.id.button2);
        btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent nexpActivityIntent = new Intent();
//				nexpActivityIntent.setClass(MainActivity.this,namefortune.class);
//				nexpActivityIntent.setClass(MainActivity.this,namefortuneFragment.class);
				nexpActivityIntent.setClass(MainActivity.this,namefortuneTab.class);
				startActivity(nexpActivityIntent);
			}
		});

        btn3=(Button)findViewById(R.id.button3);
        btn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent nexpActivityIntent = new Intent();
				nexpActivityIntent.setClass(MainActivity.this,bodyclock.class);
				startActivity(nexpActivityIntent);
			}
		});

        btn4=(Button)findViewById(R.id.button4);
        btn4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent nexpActivityIntent = new Intent();
				nexpActivityIntent.setClass(MainActivity.this,bazi.class);
				startActivity(nexpActivityIntent);
			}
		});

        btn5=(Button)findViewById(R.id.button5);
        btn5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent nexpActivityIntent = new Intent();
				nexpActivityIntent.setClass(MainActivity.this,fivemarry.class);
				startActivity(nexpActivityIntent);
			}
		});
        */
        dataAdapter = new ViewAdapter(R.layout.mainlist);
        ListView listView = (ListView) findViewById(R.id.listview);
		listView.setAdapter(dataAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent nexpActivityIntent = new Intent();
				nexpActivityIntent.setClass(MainActivity.this,classary[position]);
				startActivity(nexpActivityIntent);
			}
		});
    }
    
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if(keyCode == KeyEvent.KEYCODE_BACK  
                && event.getAction() == KeyEvent.ACTION_DOWN){  
            if((System.currentTimeMillis()-exitTime) > 2000){  
                Toast.makeText(getApplicationContext(), "再按一次后退键退出程序", Toast.LENGTH_SHORT).show();  
                exitTime = System.currentTimeMillis();  
            } else {  
                //退出代码  
            	finish();
            	System.exit(0);
            }  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    } 
    
	private class ViewAdapter extends BaseAdapter {
		private int textViewResourceId;
	
		public ViewAdapter(int textViewResourceId) {
			this.textViewResourceId = textViewResourceId;
		}
	
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater vi = (LayoutInflater) MainActivity.this
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = vi.inflate(textViewResourceId, null);
			}
			title=(TextView) convertView.findViewById(R.id.title);
			text=(TextView) convertView.findViewById(R.id.text);
			title.setText(titleary[position]);
			text.setText(textary[position]);
			return convertView;
		}

		@Override
		public int getCount() {
			return listcount;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
	}
}
